import { DatePipe } from "@angular/common";

export class DialogBase {
    beginDate: Date = new Date(+new Date() - 24 * 60 * 60 * 1000);
    endDate: Date = new Date();
    pageSize: number = 2000;

    processOpt: any = {
        tooltip: {
            trigger: "axis",
            axisPointer: {
                type: "cross",
                animation: false,
                label: {
                    backgroundColor: "#505765"
                }
            }
        },
        legend: {
            data: ["前池水位", "后池水位", "流量"],
            bottom: 10
        },
        xAxis: {
            type: "category",
            data: []
        },
        yAxis: [
            {
                name: "水位(m)",
                type: "value"
            },
            {
                name: "流量(m³/s)",
                type: "value"
            }
        ],
        series: [
            {
                name: "前池水位",
                type: "line",
                smooth: true,
                data: []
            },
            {
                name: "后池水位",
                type: "line",
                smooth: true,
                data: []
            },
            {
                name: "流量",
                type: "line",
                yAxisIndex: 1,
                smooth: true,
                data: []
            }
        ]
    };
    processOptChange: any = {};

    eigenvalues: any[] = ["最高流量", "最低流量", "平均流量"];
    eigenKeys: any[] = ["max_flux", "min_flux", "avg_flus"];
    selectedIdx: number = 0;
    timeTypes: any[] = ["旬", "月"];
    type: number = 1;
    curYear: string = "2020";
    year1: Date = new Date("2018");
    year2: Date = new Date("2019");
    monthData: any = {};
    monthOpt: any = {
        tooltip: {
            trigger: "axis",
            axisPointer: {
                type: "shadow"
            }
        },
        legend: {
            data: ["2018", "2019", "2020"],
            bottom: 0
        },
        grid: {
            top: "10%",
            bottom: "8%",
            left: "6%",
            right: "4%",
            containLabel: true
        },
        xAxis: {
            type: "category",
            data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月"]
        },
        yAxis: {
            name: "流量(m³/s)",
            type: "value"
        },
        series: []
    };
    monthOptChange: any = {};

    historyTypes: any[] = ["前池水位", "后池水位", "流量"];
    historyKeys: any[] = ["fpdLevel", "bpdLevel", "flux"];
    hType: number = 0;
    historyYear1: Date = new Date("2018");
    historyYear2: Date = new Date("2019");
    historyBegin: Date = new Date("1970-1-1");
    historyEnd: Date = new Date("1970-10-7");
    historyLists: any[] = [];
    historyOpt: any = {
        tooltip: {
            trigger: "axis",
            axisPointer: {
                type: "shadow"
            }
        },
        legend: {
            data: ["2018", "2019"]
        },
        xAxis: {
            type: "category",
            data: []
        },
        yAxis: {
            type: "value"
        },
        series: [
            {
                name: "2018",
                type: "line",
                data: []
            },
            {
                name: "2019",
                type: "line",
                data: []
            }
        ]
    };
    historyOptChange: any = {};

    constructor(public datePipe: DatePipe) { }

    onChange(dates) {
        this.beginDate = dates[0];
        this.endDate = dates[1];
    }

    onChangeHistory(dates) {
        this.historyBegin = dates[0];
        this.historyEnd = dates[1];
    }

    getItemValues(list, key, dt?) {
        return list.map(i => {
            if (dt) {
                return i[key].slice(5, -3);
            } else {
                return i[key] || 0;
            }
        });
    }

    getDateList(year) {
        year = year.getFullYear();
        const begin = this.datePipe.transform(this.historyBegin, "MM-dd");
        const end = this.datePipe.transform(this.historyEnd, "MM-dd");
        return {
            beginTime: `${year}-${begin}`,
            endTime: `${year}-${end}`
        };
    }

    checkHistoryDate() {
        if (
            this.getMonthTime(this.historyBegin) >=
            this.getMonthTime(this.historyEnd)
        ) {
            return false;
        }
        return true;
    }

    getMonthTime(date) {
        const month = date.getMonth();
        const day = date.getDate();
        return new Date(1970, month, day).getTime();
    }
}
